﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.OA.Utils;
using Ccit.Utils;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Ccit.OA.NewsCenter
{
    public partial class news_list_manage : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                _hidType.Value = (Request.QueryString["Type"] != null ) ? Request.QueryString["Type"].ToString() : "0";
                _hidTabId.Value = (Request.QueryString["TABID"] != null) ? Request.QueryString["TABID"].ToString() : "";
                BindDrop();
            }
            initialize();
        }
        private void BindDrop()
        {
            LoginUser login = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
            CtrInterface.BindDropDown(ref ddlManageNews, "NEWS_A8", "NEWS_A8_10", "NEWS_A8_40", true, "--请选择--", "NEWS_A8_9994='" + login.UserId + "' AND  NEWS_A8_60='1' AND NEWS_A8_30 = '" + _hidType.Value + "'");
        }

        private void initialize()
        {
            string sClauseXml = "";
            ColumnBLL bll = new ColumnBLL();
            SubmitBLL osubmitBll = new SubmitBLL();
            if (Session["QryClause"] == null || Session["QryClause"].ToString() == "")
            {
                string sShortcutClause = ddlManageNews.SelectedValue;
                sClauseXml = SysInterface.GetNameStrByIdStr("NEWS_A8", "NEWS_A8_10", "NEWS_A8_50", sShortcutClause, "");
            }
            else
            {
                sClauseXml = Session["QryClause"].ToString();
                Session.Remove("QryClause");
            }
            MsClauseAttr msClauseAttr = new MsClauseAttr(sClauseXml);
            List<MsNews> lstNews = osubmitBll.GetNewsManageList(msClauseAttr, _hidType.Value);
            gvNewsManagerList.DataSource = lstNews;
            gvNewsManagerList.DataBind();
        }

        protected void gvNewsManagerList_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
        {
            if (e.RowType != DevExpress.Web.ASPxGridView.GridViewRowType.Data) return;

            string sNewsId = e.KeyValue.ToString();
            string sColumnNo = gvNewsManagerList.GetRowValues(e.VisibleIndex, "ColumnNo").ToString();
            string sNewsTitle = gvNewsManagerList.GetRowValues(e.VisibleIndex, "NewsTitle").ToString();
            string sNewsStatus = gvNewsManagerList.GetRowValues(e.VisibleIndex, "NewsStatus").ToString();
            string sPicFlag = gvNewsManagerList.GetRowValues(e.VisibleIndex, "PicFlag").ToString();
            string sTopFlag = gvNewsManagerList.GetRowValues(e.VisibleIndex, "TopFlag").ToString();
            string sViewCount = gvNewsManagerList.GetRowValues(e.VisibleIndex, "ViewCount").ToString();
            string IssueDate = gvNewsManagerList.GetRowValues(e.VisibleIndex, "IssueDate").ToString();
            string CommentCount = gvNewsManagerList.GetRowValues(e.VisibleIndex, "CommentCount").ToString();

            e.Row.Cells[1].Text = "<a href=\"javascript:doViewNews('" + sNewsId + "')\">" + sNewsTitle;
            string sColumnName = SysInterface.GetNameStrByIdStr("NEWS_A2", "NEWS_A2_10", "NEWS_A2_20", sColumnNo, "");
            e.Row.Cells[2].Text = (sColumnName == "") ? "&nbsp;" : sColumnName;

            e.Row.Cells[7].Text = "<a href=\"javascript:doComment('" + sNewsId + "')\">" + CommentCount;

            if (IssueDate == "")
                e.Row.Cells[8].Text = "&nbsp;";
            if (sNewsStatus == "1")
                e.Row.Cells[3].Text = "已提交";
            else if (sNewsStatus == "2")
                e.Row.Cells[3].Text = "已发布";

            if (sNewsStatus == "1")  //已提交
            {
                if (sPicFlag == "0")
                    e.Row.Cells[4].Text = "<a href=\"javascript:doChangePic('" + sNewsId + "')\">否";
                else if (sPicFlag == "1")
                    e.Row.Cells[4].Text = "<a href=\"javascript:doChangePic('" + sNewsId + "')\">是";

                if (sTopFlag == "0")
                    e.Row.Cells[5].Text = "<a href=\"javascript:doChangeTopFlag('" + sNewsId + "','0')\">否";
                else if (sTopFlag == "1")
                    e.Row.Cells[5].Text = "<a href=\"javascript:doChangeTopFlag('" + sNewsId + "','1')\">是";
            }
            else if(sNewsStatus == "2")  //已发布
            {
                if (sPicFlag == "0")
                    e.Row.Cells[4].Text = "<a disabled='false'>否";
                else if (sPicFlag == "1")
                    e.Row.Cells[4].Text = "<a disabled='false'>是";

                if (sTopFlag == "0")
                    e.Row.Cells[5].Text = "<a disabled='false'>否";
                else if (sTopFlag == "1")
                    e.Row.Cells[5].Text = "<a disabled='false'>是";
            }

            e.Row.Cells[6].Text = "<a href=\"javascript:doShowViewInfo('" + sNewsId + "')\">" + sViewCount;

            HtmlAnchor haModify = (HtmlAnchor)gvNewsManagerList.FindRowCellTemplateControl(e.VisibleIndex, null, "haModify");
            HtmlAnchor haDelete = (HtmlAnchor)gvNewsManagerList.FindRowCellTemplateControl(e.VisibleIndex, null, "haDelete");
            if (sNewsStatus == "1")
            {
                haModify.Attributes["href"] = "javascript:doModify('" + sNewsId + "')";
                haDelete.Attributes["href"] = "javascript:doDelete('" + sNewsId + "')";
            }
            else if(sNewsStatus == "2")
            {
                haModify.Attributes["disabled"] = "false";
                haDelete.Attributes["disabled"] = "false";
                DevExpress.Web.ASPxClasses.Internal.InternalWebControl check = (DevExpress.Web.ASPxClasses.Internal.InternalWebControl)e.Row.Cells[0].Controls[0];
               // check.Attributes["disabled"] = "false";
            }
        }

        protected void lnkSearch_Click(object sender, EventArgs e)
        {
            DevExpress.Web.Data.WebDataSelection oSelection = gvNewsManagerList.Selection;
            oSelection.UnselectAll();
            initialize();
        }

        /// <summary>
        /// 操作信息
        /// </summary>
        /// <param name="sNewsIds"></param>
        /// <param name="sOperType"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string OperateNews(string sNewsIds, string sOperType, string sCurrTopFlag, string sType)
        {
            bool bFlag = false;
            SubmitBLL oSubmit = new SubmitBLL();
            if (sOperType.Equals("DELETE"))
            {
                bFlag = oSubmit.RemoveNewsByIds(sNewsIds, sType);
            }
            else if (sOperType.Equals("PUBLIC"))
            {
                bFlag = oSubmit.PublicNewsByIds(sNewsIds);
            }
            else if (sOperType.Equals("TOPFLAG"))
            {
                bFlag = oSubmit.ChangeTopFlag(sNewsIds, sCurrTopFlag);
            }
            else if (sOperType.Equals("REVOCATION"))
            {
                bFlag = oSubmit.RevocationNewsByIds(sNewsIds);
            }
            else if (sOperType == "RETURN")
            {
                bFlag = oSubmit.ReturnNewsByIds(sNewsIds);
            }
            var oResult = new { ExeResult = bFlag, OperType = sOperType };
            return JavaScriptConvert.SerializeObject(oResult);
        }
    }
}
